package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;


/**
 * Resources Locator
 * 
 * @author Julian
 *
 */
public class ResLoc {	
	private static Map<String, String> resources = new TreeMap<String, String>();
	
	private static FileNotFoundException exc = null;
	
	static {
		try {
			loadDefault();
		} catch (FileNotFoundException e) {
			exc = e;
		}
	}
	
	public static void loadDefault() throws FileNotFoundException {
		load("data/resources-default.txt");
	}
	
	public static void load(File file) throws FileNotFoundException {
		load(file.getPath());
	}
	
	public static void load(String filename) throws FileNotFoundException {
		Scanner sc = null;
		sc = new Scanner(new File(filename));
		
		String category = "";
		while(sc.hasNextLine()) {
			String nextLine = sc.nextLine();
			if(nextLine.startsWith("<") && nextLine.endsWith(">")) {
				category = nextLine.substring(1, nextLine.length() - 1);
			}
			else if( !nextLine.startsWith("</") && !nextLine.endsWith(">")) {
				String[] s = nextLine.split(":");
				String type = s[0].trim();
				String value = s[1].trim();
				resources.put(category + "." + type, value);
			}
			
		} // while end
		
		sc.close();
	}
	
	public static String getResource(String location) throws FileNotFoundException {
		if(exc == null) {
			return resources.get(location);
		} else {
			throw exc;
		}
	}
}
